﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;

namespace DIWAGIS.ApplicationLayer.EditorTools
{
    /// <summary>
    /// Command that works in ArcMap/Map/PageLayout
    /// </summary>
    [Guid("1f2bc8f7-bd5d-49aa-b30d-4abd8453d616")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("DIWAGIS.ApplicationLayer.EditorTools.cmdRedo")]
    public sealed class cmdRedo:BaseCommand
    {
        #region COM Registration Function(s)
        [ComRegisterFunction()]
        [ComVisible(false)]
        static void RegisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryRegistration(registerType);

            //
            // TODO: Add any COM registration code here
            //
        }

        [ComUnregisterFunction()]
        [ComVisible(false)]
        static void UnregisterFunction(Type registerType)
        {
            // Required for ArcGIS Component Category Registrar support
            ArcGISCategoryUnregistration(registerType);

            //
            // TODO: Add any COM unregistration code here
            //
        }

        #region ArcGIS Component Category Registrar generated code
        /// <summary>
        /// Required method for ArcGIS Component Category registration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryRegistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Register(regKey);
            ControlsCommands.Register(regKey);
        }
        /// <summary>
        /// Required method for ArcGIS Component Category unregistration -
        /// Do not modify the contents of this method with the code editor.
        /// </summary>
        private static void ArcGISCategoryUnregistration(Type registerType)
        {
            string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
            MxCommands.Unregister(regKey);
            ControlsCommands.Unregister(regKey);
        }

        #endregion
        #endregion

        private IHookHelper m_hookHelper = null;
        private IMapControl4 m_mapcontrol;
        private IOperationStack m_operationStack;
        private IEngineEditor m_engineEditor;
        private IWorkspaceEdit m_workspaceEdit;
        public IWorkspaceEdit workspaceEdit
        {
            get { return m_workspaceEdit; }
            set { m_workspaceEdit = value; }
        }
        public cmdRedo(IOperationStack operationStack)
        {
            //
            // TODO: Define values for the public properties
            //
            base.m_category = "Làm lại"; //localizable text
            base.m_caption = "Làm lại";  //localizable text 
            base.m_message = "Làm lại";  //localizable text
            base.m_toolTip = "Làm lại";  //localizable text
            base.m_name = "Làm lại";   //unique id, non-localizable (e.g. "MyCategory_MyCommand")

            m_operationStack = operationStack;
            try
            {
                //
                // TODO: change bitmap name if necessary
                //
                string bitmapResourceName = GetType().Name + ".bmp";
                base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
            }
        }

        #region Overridden Class Methods

        /// <summary>
        /// Occurs when this command is created
        /// </summary>
        /// <param name="hook">Instance of the application</param>
        public override void OnCreate(object hook)
        {
            if (hook == null)
                return;

            try
            {
                m_hookHelper = new HookHelperClass();
                m_hookHelper.Hook = hook;
                m_engineEditor = new EngineEditorClass();
                if (m_hookHelper.ActiveView == null)
                    m_hookHelper = null;
            }
            catch
            {
                m_hookHelper = null;
            }

            if (m_hookHelper == null)
                base.m_enabled = false;
            else
                base.m_enabled = true;

            // TODO:  Add other initialization code
        }

        /// <summary>
        /// Occurs when this command is clicked
        /// </summary>
        public override void OnClick()
        {
            // TODO: Add cmdRedo.OnClick implementation
            //bool hasRedo;
            //if (m_workspaceEdit != null)
            //{
            //    if (m_workspaceEdit.IsBeingEdited() == true)
            //    {
            //        hasRedo = false;
            //        m_workspaceEdit.HasRedos(ref hasRedo);
            //        if (hasRedo == true)
            //            m_workspaceEdit.RedoEditOperation();
            //    }
            //}
            //else
            //{
            for (int i = 0; i < m_operationStack.Count; i++)
            {
                IOperation Op = m_operationStack.Item[i];
                if (Op.CanRedo)
                {
                    m_operationStack.Redo();
                    break;
                }
            }
            //}
        }
        public override bool Enabled
        {
            get
            {
                //check whether Editing
                //if (m_workspaceEdit != null)
                //{
                //    if (m_workspaceEdit.IsBeingEdited() == false)
                //        return false;
                //    bool hasRedo = false;
                //    m_workspaceEdit.HasRedos(ref hasRedo);
                //    if (hasRedo == false)
                //        return false;
                //}
                //else
                //{
                if (m_engineEditor.EditState == esriEngineEditState.esriEngineStateNotEditing)
                {
                    return false;
                }
                if (m_operationStack.Count == 0)
                {
                    return false;
                }
                for (int i = 0; i < m_operationStack.Count; i++)
                {
                    IOperation Op = m_operationStack.Item[i];
                    if (Op.CanRedo == false)
                    {
                        return false;
                    }
                }
                //}
                //conditions have been met so enable the tool
                return true;
            }
        }
        #endregion
    }
}
